﻿using AutoMapper;
using Entity.System.DTO.Menu;
using Entity.System.Entity;
using Entity.System.View.Menu;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.System
{
    public class MenuService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public MenuService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取菜单列表
         */
        public async Task<RequestResponseModel<List<MenuView>>> GetMenuList(MenuSearchDTO menuSearchDTO)
        {
            //筛选
            var query = _context.MenuView
                .WhereIF(a => a.Name.Contains(menuSearchDTO.Name), !string.IsNullOrEmpty(menuSearchDTO.Name))
                .WhereIF(a => a.Url.Contains(menuSearchDTO.Url), !string.IsNullOrEmpty(menuSearchDTO.Url))
                .WhereIF(a => a.Show == menuSearchDTO.Show, menuSearchDTO.Show != null)
            ;

            //返回列表
            var MenuList = await query
            .OrderByDescending(a => a.CreateTime)
                .ToListAsync();

            //返回数据
            return RequestResponse.CreateResponse(200, "查询成功", MenuList);
        }

        /**
         * 新增菜单
         */
        public async Task<RequestResponseModel<string>> AddMenu(MenuAddDTO menuAddDTO)
        {
            //生成新数据
            var menu = _mapper.Map<MenuModel>(menuAddDTO);

            menu.Id = Guid.NewGuid().ToString();
            menu.CreateTime = DateTime.Now;

            //新增数据
            _context.Menu.Add(menu);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", menu.Id);
        }

        /**
         * 编辑菜单
         */
        public async Task<RequestResponseModel<string>> PutMenu(MenuUpdateDTO menuUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<MenuModel>(_context.Menu, menuUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", menuUpdateDTO.Id);
            };

            //编辑数据
            var menu = _mapper.Map(menuUpdateDTO, query);

            //更新表
            _context.Menu.Update(menu);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", menuUpdateDTO.Id);
        }

        /**
         * 删除菜单
         */
        public async Task<RequestResponseModel<string>> DeleteMenu(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<MenuModel>(_context.Menu, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //校验该菜单下是否有子菜单
            var menu = await _context.MenuView
                .Where(x => x.SuperiorId == Id)
                .ToListAsync();

            //如果有则返回
            if (menu.Count > 0)
            {
                return RequestResponse.CreateResponse(400, "该菜单下还有子菜单，无法删除", Id);
            };

            //删除操作
            _context.Menu.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
